package itbour.onetouchshow.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import itbour.onetouchshow.R;
import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by Yang on 2018/1/4.
 */

@Deprecated
public class OneTouchProgressDialog extends DialogFragment {


    //进度定时器
    protected Timer updateProcessTimer;
    //定时器任务
    protected ProgressTimerTask mProgressTimerTask;
    /**
     * 进度条对话框
     */
    TextView tvprogress;
    ProgressBar progressBar;
    TextView titleTv;
    int progress = 0;
    OneTouchPorgressOver oneTouchPorgressOver;
    boolean isPause = true;
    //  private OneTouchProgressDialog oneTouchProgressDialog = new OneTouchProgressDialog();
    private View rootView;
    private String title;

    @SuppressLint("ValidFragment")
    public OneTouchProgressDialog() {

    }

    /**
     * 初始化的时候用
     *
     * @return
     */
//    public static OneTouchProgressDialog getInstance() {
//        return oneTouchProgressDialog;
//    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //此处可以设置Dialog的style等等
        super.onCreate(savedInstanceState);
        //无法直接点击外部取消dialog
        setCancelable(false);
        //NO_FRAME就是dialog无边框，0指的是默认系统Theme
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.dialog_transparent_style);

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //rootView = UIUtils.inflate(R.layout.custom_progress_layout);
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.custom_progress_layout, null);
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams windowParams = window.getAttributes();
        //windowParams.dimAmount = 0.0f;
        // windowParams.y = 100;
        window.setAttributes(windowParams);
        Dialog dialog = getDialog();
        if (dialog != null) {
            int width = UIUtils.WHD()[0] / 3;
            int height = UIUtils.WHD()[0] / 4;
            dialog.getWindow().setLayout(width, height);
        }

        tvprogress = rootView.findViewById(R.id.id_pro_gress_tv);
        progressBar = rootView.findViewById(R.id.id_pro_gress);

        titleTv = rootView.findViewById(R.id.id_tv_hint);
        if (getTitle() != null) {
            titleTv.setText(getTitle());
        }

        //progressDialog.setCancelable(false);// 设置点击空白处也不能关闭该对话框
        // progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置采用圆形进度条
        progress = 0;
        progressBar.setMax(100);
        progressBar.setProgress(0);
        //progressBar.setIndeterminate(true);//设置不显示明确的进度
        progressBar.setIndeterminate(false);// 设置显示明确的进度

    }

    public TextView getTvprogress() {
        return tvprogress;
    }

    public void setTvprogress(TextView tvprogress) {
        this.tvprogress = tvprogress;
    }

    public void startProgressTimer() {
//        if (updateProcessTimer != null) {
//            updateProcessTimer.cancel();
//        }
//        if (mProgressTimerTask != null) {
//            mProgressTimerTask.cancel();
//        }
//        setPause(true);
        updateProcessTimer = new Timer();
        mProgressTimerTask = new ProgressTimerTask();
        // 6.66秒100/100
        updateProcessTimer.schedule(mProgressTimerTask, 0, 67);
    }

    public ProgressBar getProgressBar() {
        return progressBar;
    }

    /**
     * 数据加载成功后快速跑
     */
    public void startQuickProgressTimer() {
//        if (updateProcessTimer != null) {
//            updateProcessTimer.cancel();
//        }
//        if (mProgressTimerTask != null) {
//            mProgressTimerTask.cancel();
//        }
//        setPause(true);
        progress = 95;
        updateProcessTimer = new Timer();
        mProgressTimerTask = new ProgressTimerTask();
        // updateProcessTimer.cancel();
        updateProcessTimer.schedule(mProgressTimerTask, 0, 30);
    }

    public void setOneTouchPorgressOver(OneTouchPorgressOver oneTouchPorgressOver) {
        this.oneTouchPorgressOver = oneTouchPorgressOver;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void builder(Activity activity) {


        setPause(true);

        //启动线程，模拟一个耗时的操作
        startProgressTimer();
    }


    public void builderApk(Activity activity) {
        show(activity.getFragmentManager().beginTransaction(), "");

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        if (titleTv != null) {
            titleTv.setText(getTitle());
        }
    }

    @Override
    public void dismiss() {
        if (updateProcessTimer != null) {
            updateProcessTimer.cancel();
            updateProcessTimer = null;
        }
        if (mProgressTimerTask != null) {
            mProgressTimerTask.cancel();
            mProgressTimerTask = null;
        }
        if (progressBar != null) {
            progressBar.setProgress(0);
            progress = 0;
        }
        super.dismiss();
    }

    public boolean isPause() {
        return isPause;
    }

    public void setPause(boolean pause) {
        isPause = pause;
    }

    public interface OneTouchPorgressOver {
        void onMackOver();
    }

    private class ProgressTimerTask extends TimerTask {
        @Override
        public void run() {
            if (rootView == null) {
                return;
            }
            rootView.post(new Runnable() {
                @Override
                public void run() {
                    //   L_.e(progress + "---");
                    progressBar.incrementProgressBy(1);
                    if (progress < 100) {
                        if (isPause) {
                            progress++;
                        }
                        if (progress == 80 && isPause) {
                            updateProcessTimer.cancel();
                            mProgressTimerTask.cancel();
                            setPause(false);
                        }
                        tvprogress.setText(progress + "%");
                    } else if (progress >= 100 && progress < 110) {
                        progress++;
                        tvprogress.setText(100 + "%");
                        progressBar.setProgress(100);
                    } else {
                        tvprogress.setText(100 + "%");
                        progressBar.setProgress(100);
                        //updateProcessTimer.cancel();
                        //mProgressTimerTask.cancel();
                        dismiss();
                        if (oneTouchPorgressOver != null) {
                            oneTouchPorgressOver.onMackOver();
                            oneTouchPorgressOver = null;
                        }
                    }
                }
            });
        }
    }
}
